/**
 * 管理员
 * @author yangjian
 */
define(function(require, exports) {

	//加载依赖
	require("ajaxproxy");
	var common = require("common");
	var template = require("jtemplate");
	template.config("openTag", "{[");
	template.config("closeTag", "]}");

	exports.init = function(configs) {

		//初始化AjaxProxy插件
		$('.ajaxproxy').AjaxProxy({
			dataType : "json",
			successCode : "000", //成功时返回的code
			method : "post",
			formId : "content-add-form",
			formCheckHandler : function(form_id) {
				return common.formCheckHandler(form_id, true);
			},
			callbackDelay : __global.jdialog.timer,
			timeInterval : __global.jdialog.timer,
			callBack : function (data) {   /* 执行ajax之后的回调函数 */
				common.ajaxCallback(data);
			}
		});

		//初始化swich插件
		common.bswitctListInit(configs);
		common.bswitchAddInit();

		//添加数据
		$("#content-add").on("click", function() {
			common.post("添加菜单", "content-add-template", {width:600, height:500}, {ishow:'1'}, configs.insert_url);
		});

		//编辑数据
		$(".item-edit").on("click", function() {

			$.get(configs.edit_url, {
				id : $(this).data("id")
			}, function(res) {
				if ( res.code == '000' ) {

					common.post("编辑菜单", "content-add-template", {width:600, height:500}, res.data, configs.update_url);

				} else {
					common.message({
						type: __global.jdialog.messageType[res.code],
						content:res.message,
						lock : true
					});
				}
			}, 'json')

		});

		//批量审核
		$("#enabled").on("click", function() {
			exports.set(configs, this);
		});

		$("#disabled").on("click", function() {
			exports.set(configs, this);
		});

		$('#remove').on("click", function() {

			if ( $('#J_List_Form').find('input[name="ids\[\]"]:checked').length == 0 ) {
				common.message({
					type:"warn",
					top : 70,
					content:"请至少选中一条记录"
				});
				return;
			}

			var formData = $("#J_List_Form").serializeArray();
			var button = $(this).button("loading"); //锁定按钮防止重复点击
			$.post(configs.dels_url, formData, function(data) {
				if ( data.code == '000' ) {
					common.message({
						type:"ok",
						content:data.message,
						lock:true
					});

					//刷新页面
					setTimeout(function() {
						location.reload();
					}, __global.jdialog.timer)
				} else {
					common.message({
						type:"error",
						top : 70,
						content:data.message,
						lock:false
					});

					button.button("reset");
				}
			}, 'json');

		});

		//初始化排序
		exports.sort(configs);
	}

	//菜单排序
	exports.sort = function(configs) {
		//往上移动菜单
		$(".arrow-up").on('click', function() {
			var org = $(this).parents('tr'); //源元素
			var dist = null; //目标元素
			var id = org.data("id");
			if ( org.data('pid') > 0 ) { //二级菜单
				if ( org.prev().data('pid') == 0 ) {
					return false;
				}
				dist = org.prev();
				org.insertBefore(dist);

			} else { //一级菜单
				var dist = org.prevAll('[data-pid="0"]').first();
				org.insertBefore(dist);
				var lastNode = org; //当前一级菜单最后一个子节点
				//移动子类二级菜单
				$('[data-pid="'+id+'"]').each(function(index, element) {
					$(element).insertAfter(lastNode);
					lastNode = element;
				});

			}

			exports.exchangeSort(configs, org, dist);

		});

		//往下移动菜单
		$(".arrow-down").on('click', function() {
			var org = $(this).parents('tr'); //源元素
			var dist = null; //目标元素
			var id = org.data("id");

			if ( org.data('pid') > 0 ) { //二级菜单
				if ( org.next().data('pid') == 0 ) {
					return;
				}
				dist = org.next();
				org.insertAfter(dist);

			} else { //一级菜单
				var dist = org.nextAll('[data-pid="0"]').first();
				var pid = dist.data('id');
				//获取下一个一级菜单的最后的子节点
				var lastNode = $('[data-pid="'+pid+'"]').last();
				if ( lastNode.length == 0 ) { //只有一级菜单
					org.insertAfter(dist);
				} else {
					org.insertAfter(lastNode);
					lastNode = org;
					//移动子类二级菜单
					$('[data-pid="'+id+'"]').each(function(index, element) {
						$(element).insertAfter(lastNode);
						lastNode = element;
					});
				}
			}
			exports.exchangeSort(configs, org, dist);

		});
	}

	//交换菜单排序
	exports.exchangeSort = function(configs, org, dist) {

		var data = {
			org_id : org.data("id"),
			org_sort : dist.data("sort"),
			dist_id : dist.data("id"),
			dist_sort : org.data("sort")
		}
		//console.log(data);
		$.post(configs.exchange_sort_url, data, function(data) {
			//console.log(data);
			if ( data.code == '000' ) {
				common.message({type:"ok", top:__global.jdialog.top, content:"移动菜单成功."});
			} else {
				common.message({type:'error', top:__global.jdialog.top, content:'移动菜单失败.'});
			}
		}, 'json');

	}

});
